//package top.xierongsheng.user.controller;
//
//import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.tags.Tag;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.web.bind.annotation.*;
//import top.xierongsheng.code.CacheVariable;
//import top.xierongsheng.code.R;
//import top.xierongsheng.code.redis.RedisService;
//import top.xierongsheng.code.wx.EventProcessor;
//import top.xierongsheng.user.dto.memberDto;
//import top.xierongsheng.user.model.MEMBER;
//import top.xierongsheng.user.service.IMemberService;
//import top.xierongsheng.utlis.data.RandomUtil;
//
//import java.util.Date;
//import java.util.Map;
//
//@Slf4j
//@RestController
//@RequestMapping(path = "/member")
//@RequiredArgsConstructor
//@CrossOrigin
//@Tag(name = "会员接口")
//public class MemberController {
//
//    private final IMemberService memberService;
//
//    private final RedisService redisService;
//
//    /**
//     * 微信会员用户认证登录
//     *
//     * @param userId
//     * @param code
//     * @return
//     */
//    @GetMapping(path = "wxLogin")
//    @Operation(summary = "微信会员用户认证登录")
//    public
//    R login(@RequestHeader(name = "userId", required = false) String userId, @RequestParam String code) {
//        //获取微信登录信息
//        Map wxInfo = EventProcessor.code2Session(code);
//        String openid = wxInfo.get("openid").toString();
//        String unionid = wxInfo.get("unionid").toString();
//
//        if (openid.equals("null")) {
//            log.error("----------------------微信登录认证接口异常 未获取到openID-------------------------------");
//            return R.error();
//        }
//
//        //通过openid获取用户id
//        String id = memberService.getOneByOpenid(openid);
//        if (id == null) {
//            id = RandomUtil.getUUID();
//            MEMBER member = new MEMBER();
//            member.setId(id);
//            member.setCreateTime(new Date());
//            member.setOpenId(openid);
//            member.setStatus(1);
//            member.setUnionId(unionid);
//            member.setUpdateTime(new Date());
//            memberService.save(member);
//        }
//        redisService.set(CacheVariable.WX_USER + id, id, CacheVariable.WX_USER_EXPIRE);
//        return R.ok().zip("data", id);
//    }
//
//    /**
//     * 更新会员用户信息
//     *
//     * @param userId
//     * @param memberDto
//     * @return
//     */
//    @PostMapping(path = "updateMemberInfo", consumes = {"application/json", "application/x-www-form-urlencoded"})
//    @Operation(summary = "更新会员用户信息")
//    public R updateMemberInfo(@RequestHeader(name = "userId") String userId, @RequestBody memberDto memberDto) {
//        MEMBER member = memberService.getById(userId);
//        if(member == null){
//            return R.fail("小程序用户不存在");
//        }
//        member.setAvatar(memberDto.getAvatar());
//        member.setIdCard(memberDto.getIdCard());
//        member.setName(memberDto.getName());
//        member.setPhone(memberDto.getPhone());
//        member.setNickname(memberDto.getNickname());
//        member.setUpdateTime(new Date());
//        memberService.updateById(member);
//        return R.ok("小程序用户信息更新成功");
//    }
//
//}
